<template>
  <div class="app-container">
    <!-- 分派对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="客户名称" prop="name">
          <el-input v-model="form.names" readonly/>
        </el-form-item>
        <el-form-item label="跟进人" prop="userId">
          <el-select v-model="form.userId" filterable  placeholder="请选择" @change="handleChange">
            <el-option
              v-for="user in userList"
              :key="user.userId"
              :label="user.userName"
              :value="user.userId"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { sendCustom } from "@/api/custom/custom";
import { listUser } from '@/api/system/user'
export default {
  name: "CustomSend",
  data() {
    return {
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {
      },
      selectedItem: null,
      userList:[],
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "请选择跟进人！", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    handleChange(value) {
     console.log(value);
      this.selectedItem = this.userList.find(item => item.userId === value) || null;
     this.form.userName = this.selectedItem.userName;
    },
    // 表单重置
    reset() {
      this.form = {
        ids: null,
        names:null,
        userId: null,
        userName:null,
      };
      this.resetForm("form");
    },
    getUserList(){
      listUser({}).then(response => {
          this.userList = response.rows
        }
      )
    },
    /** 分派客戶操作 */
    handleSend(ids,names) {
      this.reset();
      this.form.ids = ids;
      this.form.names = names;
      this.open = true;
      this.title = "分派客戶";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          console.log(this.form);
          sendCustom(this.form).then(response => {
            this.$modal.msgSuccess("分派成功");
            this.open = false;
            this.$emit('refresh')
          });
        }
      });
    },
  }
};
</script>
